﻿#pragma once
#include "XyNet/Channel/Connection/RpcConnection.h"
#include "XyNet/Crypto/Aes.h"
#include "XyNet/Crypto/Ecc.h"

namespace XyNet
{
	/// 加密的RPC会话
	class XYNET_API RpcCryptoConnection : public RpcConnection
	{
	public:
		RpcCryptoConnection(RpcSocketChannel* socketChannel);
		virtual ~RpcCryptoConnection();

	protected:
		virtual void start() override;
		virtual void write(const Bytes& data) override;
		virtual void read() override;

	private:
		/// 处理读取公钥
		void handleReadEccPublicKey(const Bytes& data);
		/// 处理读取aes密钥
		void handleReadAesKey(const Bytes& data);
		/// 处理读取加密数据
		void handleReadEncryptedData(const Bytes& data);

	private:
		/// 本地生成的ECC解密器
		unique_ptr<Ecc::Decryptor> _eccDecryptor;
		/// 由远端公钥生成的ECC加密器
		unique_ptr<Ecc::Encryptor> _eccRemoteEncryptor;
		/// 本地生成的AES加密器
		unique_ptr<Aes> _aesEncryptor;
		/// 由远端密钥生成的AES解密器
		unique_ptr<Aes> _aesRemoteDecryptor;
	};
}